﻿@{ 
    Layout = "~/Views/Shared/_ModalForm.cshtml";
    var excludedArticleTypeNames = ArticleType.HttpFileShortcut.ToString() + " " + ArticleType.ArticleShortcut.ToString();
 }

<div class="wrapper animated fadeInRight">
    <form id="form" class="form-horizontal m">
        <div class="form-group">
            <label class="col-sm-3 control-label">类别</label>
            <div class="col-sm-8 " id="type" col="Type"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">标题<font class="red"> *</font></label>
            <div class="col-sm-8">
                <input id="name" col="Name" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">状态</label>
            <div class="col-sm-8" id="status" col="Status"></div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">资料夹</label>
            <div class="col-sm-8">
                <div id="masterId" col="MasterId"></div>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">排序码</label>
            <div class="col-sm-8">
                <input id="sequence" col="Sequence" type="text" class="form-control" />
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">描述</label>
            <div class="col-sm-8">
                <textarea id="description" col="Description" class="form-control" style="height:60px"></textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label ">说明</label>
            <div class="col-sm-8">
                <input id="note" col="Note" type="text" class="form-control" />
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    var id = ys.request("id");
    var act = 'edit';
    if (id == ''|id==null) act = 'add';
    var masterId = ys.request("masterId");

    $(function () {
        $("#type").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(ArticleType).EnumToDictionaryJson(excludedArticleTypeNames, true))) });
        $("#status").ysRadioBox({ data: ys.getJson(@Html.Raw(typeof(StatusType).EnumToDictionaryJson())) });
        $('#masterId').ysComboBoxTree({ url: '@Url.Content("~/Scc/Article/GetCategoryTreeJsonForSelectMaster")' });
        getForm();
        $("#form").validate({
            rules: {
                name: { required: true, maxlength: 99 },
                note: {maxlength: 99 },
                //masterId: { required: true },//no use
            }
        });
    });

    function getForm() {
        if (act == 'add') {//add
            ys.ajax({
                url: '@Url.Content("~/Scc/Article/GetMaxSequenceNoJson")' + '?masterId=' + masterId,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        var defaultData = {};
                        defaultData.Sequence = rst.Data;
                        defaultData.Type = "@((int)ArticleType.RichText)";
                        defaultData.Status = "@((int)StatusType.Enabled)";
                        defaultData.MasterId = masterId;
                        if (!masterId) {
                            $("#masterId_input").val("请选择资料夹");
                        }
                        $("#form").setWebControls(defaultData);
                    }
                    else ys.msgError(rst.Message);
                }
            });


        }
        else {//edit
            ys.ajax({
                url: '@Url.Content("~/Scc/Article/GetEditDtoJson")' + '?id=' + id,
                type: "get",
                success: function (rst) {
                    if (rst.Flag == 1) {
                        var result = rst.Data;
                        $("#form").setWebControls(result);
                    }
                    else ys.msgError(rst.Message);
                }
            });
        }
    }

    function saveForm(index) {
        if(!ys.checkTextLenth("description", "描述", 0, 127)) return;
        if (!ys.checkSelectedValue("masterId_input", "目录", "请选择目录")) return;

        if ($("#form").validate().form()) {
            var postData = $("#form").getWebControls({ Id: id });
            postData.MasterId = ys.getLastValue(postData.MasterId);
            ys.ajax({
                url: '@Url.Content("~/Scc/Article/")' + act,
                type: "post",
                data: postData,
                success: function (rst) {
                    if (rst.Flag == 1) {
                        ys.msgSuccess(rst.Message);
                        parent.searchGrid();
                        parent.layer.close(index);
                    }
                    else {
                        ys.msgError(rst.Message);
                    }
                }
            });
        }
    }
</script>
